package com.lkj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.lkj.model.Lkj;
import com.lkj.model.Train;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lkj.model.Video;
import com.lkj.service.ClientService;
import com.lkj.utils.PageUtil;

@RequestMapping("/client")
@Controller
public class ClientController {

	private static final int PAGESIZE = 4;
	
	@Resource
	private ClientService videoService;
	
	/**
	 * 跳转至列表页
	 * @return
	 */
	@RequestMapping("/videoList")
	public String videoList(){
		return "new/videoList";
	}
	
	/**
	 * 
	 * 获取视频列表
	 * @param pageUtil
	 * @return
	 */
	@RequestMapping("/getVideos")
	@ResponseBody
	public Map<String, Object> getVideos(PageUtil pageUtil){
        Map<String, Object> map = new HashMap<String, Object>();
        pageUtil.setPagesize(PAGESIZE);
        pageUtil.setTotalnum(videoService.getTotalRows(pageUtil));
        List<Video> list = videoService.getVideos(pageUtil);
        map.put("list", list);
        map.put("pageUtil", pageUtil);
		return map;
	}
	
	/**
	 * 跳转至视频详情页
	 * @return
	 */
	@RequestMapping("/videoDetail")
	public String videoDetail(Model model,String id){
		model.addAttribute("id",id);
		return "new/videoDetail";
	}

	@RequestMapping(value="/getDetailInfo",method = RequestMethod.POST)
	@ResponseBody
    public Map<String,Object> getDetailInfo(String id){
	    Map<String,Object> map = new HashMap<String, Object>();

        List<Video> list = videoService.videoDetail(id);
        List<Lkj> lkjList = videoService.getLkjInfo(id);

        List<String> timeArray1 = new ArrayList<String>();
        List<Integer> statusArray = new ArrayList<Integer>();
        List<List<Object>> status1 = new ArrayList<List<Object>>();
        List<List<Object>> status2 = new ArrayList<List<Object>>();
        List<List<Object>> status3 = new ArrayList<List<Object>>();
        List<List<Object>> status4 = new ArrayList<List<Object>>();
        List<List<Object>> status5 = new ArrayList<List<Object>>();
        List<List<Object>> status6 = new ArrayList<List<Object>>();


        for (Video video : list) {
            List<Object> tempMap = new ArrayList<Object>();
            tempMap.add(video.getStarttime());
            tempMap.add(video.getStatusid());
            timeArray1.add(video.getStarttime());
            switch (video.getStatusid()){
                case 1 : status1.add(tempMap);break;
                case 2 : status2.add(tempMap);break;
                case 3 : status3.add(tempMap);break;
                case 4 : status4.add(tempMap);break;
                case 5 : status5.add(tempMap);break;
                case 6 : status6.add(tempMap);break;
            }
        }



        List<String> timeArray = new ArrayList<String>();
        List<Integer> speedArray = new ArrayList<Integer>();
        for (Lkj lkj : lkjList) {
            timeArray.add(lkj.getStarttime());
            speedArray.add(lkj.getSpeed());
        }

        map.put("list",list);
        map.put("timeArray1",timeArray1);
        map.put("statusArray",statusArray);
        map.put("timeArray",timeArray);
        map.put("speedArray",speedArray);

        map.put("status1",status1);
        map.put("status2",status2);
        map.put("status3",status3);
        map.put("status4",status4);
        map.put("status5",status5);
        map.put("status6",status6);
	    return map;
    }


	@RequestMapping("/lkjDetail")
	public String lkjDetail(Model model,String id){
		List<Video> list = videoService.videoDetail(id);
		model.addAttribute("id",id);
		//model.addAttribute("list",list);
		return "new/lkjDetail";
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().removeAttribute("user");
		return "redirect:/login.jsp";
	}


    /**
     * 今日到达页跳转
     * @return
     */
    @RequestMapping("/todayArrive")
    public String todayArrive(){
        return "new/todayArrive";
    }


    /**
     * 客户端今日到达数据
     * @return
     */
    @RequestMapping(value = "getTrainInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getTrainInfo(){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Train> list = videoService.getTrainInfo();
        map.put("trains",list);
        return map;
    }

    /**
     * 统计汇总页跳转
     * @return
     */
    @RequestMapping("/dataSummary")
    public String dataSummary(){
        return "new/dataSummary";
    }

    /**
     * 客户端违规操作单跳转
     * @return
     */
    @RequestMapping("/irregularities")
    public String irregularities(){
        return "new/irregularities";
    }

    /**
     * 客户端违规操作单
     * @return
     */
    @RequestMapping(value = "getTrainSInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getTrainSInfo(){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Train> list = videoService.getTrainSInfo();
        map.put("trainsS",list);
        return map;
    }

}
